/**
 * Created by liubin on 2015/9/16.
 */
'use strict';

import Immutable from 'immutable';
import classNames from 'classnames';
import React from 'react';
import SidebarMixin from 'global/jsx/sidebar_component';
import Header from 'common/header';
import Sidebar from 'common/sidebar';
import Footer from 'common/footer';
import { Authorization } from 'actions/authAction';
const { ANONYMOUS } = Authorization;

class Body extends React.Component {
    static propTypes = {
        authActions: React.PropTypes.object.isRequired
    }
    render() {
        let props = Immutable.Map(this.props);
        const children = props.get('children');
        props = props.delete('children').toObject();
        return (
            <Container id='body'>
                { React.cloneElement(children, props) }
            </Container>
        );
    }
}

@SidebarMixin
class Base extends React.Component {
    static displayName = 'Base';
    static propTypes = {
        authActions: React.PropTypes.object.isRequired,
        history: React.PropTypes.object.isRequired
    }
    componentWillMount() {
        // 确认是否已经登陆
        const { authActions:{ auth } } = this.props;
        auth();
    }
    componentWillReceiveProps(nextProps){
        const { authorization } = nextProps;
        // 判断登陆
        if (authorization.authed === ANONYMOUS) {
            this.props.history.replaceState(null, '/login');
        }
    }
    render() {
        const classes = classNames({
            'container-open': this.props.open
        });
        const { authActions: { logout } } = this.props;
        return (
            <Container id='container' className={classes}>
                <Sidebar />
                <Header logout={logout}/>
                <Body {...this.props}/>
                <Footer />
            </Container>
        );
    }
}
export default Base;